//package com.learn.framework.shiro.web.filter.online;
//
//import java.io.IOException;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//
//import org.apache.shiro.session.Session;
//import org.apache.shiro.subject.Subject;
//import org.apache.shiro.web.filter.AccessControlFilter;
//import org.apache.shiro.web.util.WebUtils;
//import com.learn.common.cons.ShiroConstants;
//import com.learn.common.enums.OnlineStatus;
//import com.learn.common.utils.security.ShiroUtils;
//import com.learn.framework.shiro.session.OnlineSessionDAO;
//import com.learn.framework.spring.ApplicationUtils;
//import com.learn.framework.springboot.properties.ShiroProperties;
//import com.learn.project.monitor.online.domain.OnlineSession;
//import com.learn.project.system.user.domain.User;
//import org.springframework.beans.factory.annotation.Autowired;
//
///**
// * 自定义访问控制
// *
// * @author Crown
// */
//public class OnlineSessionFilter extends AccessControlFilter {
//
//    @Autowired
//    private OnlineSessionDAO onlineSessionDAO;
//
//    /**
//     * 表示是否允许访问；mappedValue就是[urls]配置中拦截器参数部分，如果允许访问返回true，否则false；
//     */
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        Subject subject = getSubject(request, response);
//        if (subject == null || subject.getSession() == null) {
//            return true;
//        }
//        Session session = onlineSessionDAO.readSession(subject.getSession().getId());
//        if (session instanceof OnlineSession) {
//            OnlineSession onlineSession = (OnlineSession) session;
//            request.setAttribute(ShiroConstants.ONLINE_SESSION, onlineSession);
//            // 把user对象设置进去
//            boolean isGuest = onlineSession.getUserId() == null || onlineSession.getUserId() == 0L;
//            if (isGuest) {
//                User user = ShiroUtils.getSysUser();
//                if (user != null) {
//                    onlineSession.setUserId(user.getUserId());
//                    onlineSession.setLoginName(user.getLoginName());
//                    onlineSession.setAvatar(user.getAvatar());
//                    onlineSession.setDeptName(user.getDept().getDeptName());
//                    onlineSession.markAttributeChanged();
//                }
//            }
//
//            return onlineSession.getStatus() != OnlineStatus.off_line;
//        }
//        return true;
//    }
//
//    /**
//     * 表示当访问拒绝时是否已经处理了；如果返回true表示需要继续处理；如果返回false表示该拦截器实例已经处理了，将直接返回即可。
//     */
//    @Override
//    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
//        Subject subject = getSubject(request, response);
//        if (subject != null) {
//            subject.logout();
//        }
//        saveRequestAndRedirectToLogin(request, response);
//        return false;
//    }
//
//    // 跳转到登录页
//    @Override
//    protected void redirectToLogin(ServletRequest request, ServletResponse response) throws IOException {
//        WebUtils.issueRedirect(request, response, ApplicationUtils.getBean(ShiroProperties.class).getLoginUrl());
//    }
//}
